<template>
<div style="width: 100%">
  <a-card class="card" :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="院系">
              <a-select :disabled="selectMenu.yx.length === 1" showSearch optionFilterProp="children" v-model="queryParam.ssyxm" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+"-"+ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="招生年度">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zsxn">
                <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" style="text-align:center">
            <a-button type="primary" @click="handleReport" :disabled="editable">上报</a-button>
            <a-button type="primary" @click="handleCreate" :disabled="editable">添加</a-button>
            <a-button type="primary" @click="loadData">查询</a-button>
            <a-button type="primary" @click="exportPDF" :disabled="!editable">导出PDF</a-button>
            <a-button type="primary" @click="exportShow" :disabled="editable">导出预览PDF</a-button>
            <a-button type="primary" @click="handleBatchDelete" :disabled="editable">批量删除</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    </a-card>
  <a-alert class="card"  message="※请维护完本院系所有信息后再上报、一旦上报禁止再进行任何修改" banner />
  <a-card class="card" :bordered="false">
    <a-table ref="table" size="default" rowKey="key" :columns="columns" :loading="loading"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
     :dataSource="data" :pagination="pagination" @change="handleTableChange">
      <template slot="action" slot-scope="text,record">
        <!-- <a @click="record.sbzt!='1' && handleEdit(record.id)" :style="record.sbzt!='1'?'':'color:#999'">修改</a>
        <a @click="record.sbzt!='1' && handleDelete(record.mxId?record.mxId:record.id)" :style="record.sbzt!='1'?'':'color:#999'">删除</a> -->
        <a @click="!editable && handleEdit(record.id)" :style="!editable?'':'color:#999'">修改</a>
        <a @click="!editable && handleDelete(record.mxId?record.mxId:record.id)" :style="!editable?'':'color:#999'">删除</a>
      </template>

      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
    </a-table>
    <!-- 新增 -->
    <directory-add ref="collegeAdd" @close="handleCreateClose" @success="handleCreateSuccess" :addVisiable="addVisiable" :keyNum="keyNum" ></directory-add>
    <!-- 编辑 -->
    <directory-edit ref="collegeEdit" @close="handleEditClose" @success="handleEditSuccess" :info="editData"  :editVisible="editVisible"></directory-edit>
  </a-card>
</div>
</template>

<script>
// import moment from 'moment'
import directoryAdd from './setByDirectionAdd'
import directoryEdit from './setByDirectionEdit'
import { STable } from '~'
import { mapState } from 'vuex'

const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']

export default {
  components: {
    STable,
    directoryEdit,
    directoryAdd
  },
  data () {
    return {
      loading: false,
      data: [],
      keyNum:'',
      // 查询参数
      queryParam: {
        ssyxm: '',
        zsxn:'',
        pageNum:1
      },
      postParams: {},
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '院系',
          dataIndex: 'ssyxm',
          width: 80
        },
        {
          title: '专业代码',
          dataIndex: 'sszy',
          width: 100
        },
        {
          title: '专业',
          dataIndex: 'sszym',
          width: 200
        },
        {
          title: '研究方向码',
          dataIndex: 'yjfxmmt',
          width: 150
        },
         {
          title: '研究方向',
          dataIndex: 'yjfxm',
          width: 150
        },
        {
          title: '导师',
          dataIndex: 'dsxxId',
          width:350
        },
        {
          title: '招生人数',
          dataIndex: 'zsrs'
        },
        {
          title: '考试科目',
          dataIndex: 'kskm'
        },
        {
          title: '备注',
          dataIndex: 'note'
        },
        {
          title: '操作',
          dataIndex: 'cz',
          fixed: 'right',
          width: 120,
          scopedSlots: { customRender: 'action' }
        }
      ],
       selectedRowKeys:[],
       selectedRows:[],
       options: {
         alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
         rowSelection: {
           selectedRowKeys: this.selectedRowKeys,
            onChange: (selectedRowKeys, selectedRows) => {
             console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows)
             this.selectedRowKeys = selectedRowKeys
             this.selectedRows = selectedRows
           },
           onSelect: (record, selected, selectedRows) => {
             console.log(record, selected, selectedRows)
           },
           onSelectAll: (selected, selectedRows, changeRows) => {
             console.log(selected, selectedRows, changeRows)
           },
         }
       },
      addVisiable: false,
      editVisible: false,
      selectMenu: {
        yx: [] // 院系
      },
      editData: {},
      pagination: {
        current: 1
      },
      editable:true
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'] // 年级
    })
  },
  created () {
    this.initSelectMenu()
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.queryParam.ssyxm = res[0].data[0].dwh
         // 获取当前学年
        this.$api.base.getSchoolYear().then(res=>{
          this.queryParam.zsxn = res.data.masterAdmissionYear
           this.loadData({
            pageSize: 10,
            pageNum: 1
          })
        })
      })
    },
      // 加载数据
    loadData(parameter = {}) {
      if(!parameter.pageNum){
        this.pagination.current = 1
        parameter.pageNum = 1
      }
      let params = Object.assign(this.queryParam, parameter)
      this.loading = true;
      this.$api.recruitstat.setUpByDirectoriesList(params).then(res => {
        // console.log(res)
         const pagination = { ...this.pagination
        };
        pagination.total = 0
        if (res.data && res.data.total) {
          pagination.total = res.data.total;
        }
        this.data = []
        this.pagination = pagination;
        if (res.data && res.data.rows.length) {
          this.data = res.data.rows;
          this.data.forEach(item => {
            item.yjfxmmt = item.yjfxmmt.slice(-2)
          })
        }
        let params={ssyxm:this.queryParam.ssyxm,zsxn:this.queryParam.zsxn}
        this.$api.recruitstat.ZsFxSsszszymlAble(params).then(res =>{
          this.editable=res.data;
        })
        
        this.loading = false;
      }).finally(() =>{
        this.loading = false;
      })
    },
    // 分页
    handleTableChange(pagination, filters, sorter) {
      console.log(pagination);
      const pager = { ...this.pagination
      };
      pager.current = pagination.current;
      this.pagination = pager;
      this.loadData({
        pageSize: 10,
        pageNum: pagination.current
      });
    },
     // 多选
    onSelectChange (selectedRowKeys,selectedRows) {
      console.log('selectedRowKeys changed: ', selectedRowKeys);
      this.selectedRows = selectedRows
      this.selectedRowKeys = selectedRowKeys
    },
    // 选择院系
    yxChange (code) {
    },
    // 选择招生年度
    zsndChange () {

    },
    // 添加
    handleCreate () {
      this.keyNum = Math.random();
      this.addVisiable = true
    },
    // 关闭添加弹层
    handleCreateClose () {
      this.addVisiable = false
       this.loadData({
        pageSize:10,
        pageNum:this.pagination.current
      })
    },
    // 添加成功
    handleCreateSuccess () {
      this.addVisiable = false
      this.$message.success('添加成功')
      this.loadData({
        pageSize:10,
        pageNum:this.pagination.current
      })
    },
    // 修改
    handleEdit (key) {
      this.$api.recruitstat.setUpByDirectoriesId(key).then( res => {
        this.editData = res.data
        this.editVisible = true
      })
    },
    // 关闭添加弹层
    handleEditClose () {
      this.editVisible = false
      this.loadData({
        pageSize:10,
        pageNum:this.pagination.current
      })
    },
    // 添加成功
    handleEditSuccess () {
      this.editVisible = false
      this.$message.success('添加成功')
      this.loadData({
        pageSize:10,
        pageNum:this.pagination.current
      })
    },
    // 导出
    exportExcel () {
      console.log('导出')
    },
    // 删除
    handleDelete (key) {
      console.log('删除')
      var that = this;
       this.$confirm({
         title: '确定删除?',
         onOk() {
           console.log('OK');
           that.deleteData(key)
         },
         onCancel() {
           console.log('Cancel');
         },
         class: 'test',
       });
    },
    // 批量删除
    handleBatchDelete () {
      console.log('批量删除')
         var deleteId = ''
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要报到的学生')
        return
      }
      if(this.selectedRows && this.selectedRows.length){
        deleteId = this.selectedRows.map(v => {
          return v.mxId ? v.mxId:v.id;
        }).toString()
      }
       var that = this;
       this.$confirm({
         title: '确定删除?',
         onOk() {
           console.log('OK');
           that.deleteData(deleteId)
         },
         onCancel() {
           console.log('Cancel');
         },
         class: 'test',
       });
    },
    deleteData(code){
      this.$api.recruitstat.setUpByDirectoriesDelete(code).then( res => {
         this.loadData({
          pageSize:10,
          pageNum:1
        })
        this.selectedRowKeys = []
      })
    },
    // 导出PDF
    exportPDF () {
      console.log('导出PDF')
      this.$export('admissions/zsFxSsszszyml/export', this.queryParam)
    },
    exportShow(){
      this.$export('admissions/zsFxSsszszyml/ylexport', this.queryParam)
    },
    // 上报
    handleReport () {
      console.log('上报')
      var that = this;
       this.$confirm({
         title: '确定上报?',
         onOk() {
           console.log('OK');
           that.$api.recruitstat.setUpByDirectoriesUp(that.queryParam).then(res => {
               that.loadData({
                pageSize:10,
                pageNum:that.pagination.current
              })
            })
         },
         onCancel() {
           console.log('Cancel');
         },
         class: 'test',
       });

    }
  }
}
</script>

<style lang="less" scoped>
.card{
  margin-bottom: 20px;
}
</style>
